package cn.edu.gduf.luowowo.article.service.impl;

import cn.edu.gduf.luowowo.article.domain.*;
import cn.edu.gduf.luowowo.article.mapper.StrategyContentMapper;
import cn.edu.gduf.luowowo.article.mapper.StrategyDetailMapper;
import cn.edu.gduf.luowowo.article.query.StrategyDetailQuery;
import cn.edu.gduf.luowowo.article.service.IDestinationService;
import cn.edu.gduf.luowowo.article.service.IStrategyDetailService;
import cn.edu.gduf.luowowo.article.service.IStrategyTagService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {
    @Autowired
    private StrategyDetailMapper strategyDetailMapper;
    @Autowired
    private StrategyContentMapper strategyContentMapper;
    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private IStrategyTagService strategyTagService;

    @Override
    public PageInfo query(StrategyDetailQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        return new PageInfo(strategyDetailMapper.selectForList(qo));
    }

    @Override
    public List<StrategyDetail> list() {
        return strategyDetailMapper.selectAll();
    }

    @Override
    public StrategyDetail get(Long id) {
        return strategyDetailMapper.selectByPrimaryKey(id);
    }

    @Override
    public void saveOrUpdate(StrategyDetail detail, String tags) {
        //关联目的地
        Destination dest = new Destination();
        Strategy strategy = detail.getStrategy();
        if (strategy != null && strategy.getId() != null) {
            dest = destinationService.queryByStrategyId(strategy.getId());
            detail.setDest(dest);
        }
        //作者 TODO：后台管理系统登录操作还没做
        //摘要
        StrategyContent strategyContent = detail.getStrategyContent();
        String content = strategyContent.getContent();
        if (content != null && content.length() > 200) {
            detail.setSummary(content.substring(0, 200));
        } else {
            detail.setSummary(content);
        }
        //国内外,国外true，国内false
        List<Destination> destinations = destinationService.getToasts(dest.getId());
        detail.setIsabroad(destinations.get(0).getId() != 1L);

        if (detail.getId() == null) {//添加
            //创建时间
            detail.setCreateTime(new Date());
            strategyDetailMapper.insert(detail);
            //文章内容处理
            strategyContent.setId(detail.getId());
            strategyContentMapper.insert(strategyContent);
            //标签（添加，维护关系）
            insertTagRelation(detail, tags);
        } else {//更新
            strategyDetailMapper.updateByPrimaryKey(detail);
            //内容更新
            strategyContent.setId(detail.getId());
            strategyContentMapper.updateByPrimaryKey(strategyContent);
            //标签（移除，添加，维护关系）
            strategyDetailMapper.removeRelation(detail.getId());
            insertTagRelation(detail, tags);
        }
    }

    private void insertTagRelation(StrategyDetail detail, String tags) {
        if (tags.length() > 0) {
            //查询所有标签的name集合
            List<String> names = strategyTagService.listForNames();
            String[] ts = tags.split(",");
            for (String t : ts) {
                if (!names.contains(t)) {
                    StrategyTag tag = new StrategyTag(t);
                    strategyTagService.saveOrUpdate(tag);
                    strategyDetailMapper.insertRelation(tag.getId(), detail.getId());
                } else {
                    strategyDetailMapper.insertRelation(strategyTagService.queryByTagName(t).getId(), detail.getId());
                }
            }
        }
    }

    @Override
    public StrategyContent getContent(Long id) {
        return strategyDetailMapper.queryContentByDetailId(id);
    }

    @Override
    public List<StrategyDetail> queryByCatalogId(Long catalogId) {
        return strategyDetailMapper.selectByCatalogId(catalogId);
    }

    @Override
    public List<StrategyDetail> queryDetailViewNumTop3(Long destId) {
        return strategyDetailMapper.selectDetailViewNumTop3(destId);
    }

    @Override
    public PageInfo query1(StrategyDetailQuery qo) {
        List<Long> detailIds = strategyDetailMapper.selectDetailIdByTagId(qo);
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        return new PageInfo(strategyDetailMapper.selectForList1(qo,detailIds));
    }

    @Override
    public void updateStrategyDetailForStatis(StrategyDetail detail) {
        strategyDetailMapper.updateStrategyDetailForStatis(detail);
    }

    @Override
    public List<StrategyDetail> queryDetailIdAndSubTitle() {
        return strategyDetailMapper.selectIdAndSubTitle();
    }

    @Override
    public List<StrategyDetail> queryDetailUnaboardViewNumTop5() {
        return strategyDetailMapper.selectDetailUnaboardViewNumTop5();
    }

}
